package com.aizhixin.lab.course.course.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.aizhixin.lab.common.utils.PageJdbcUtil;
import lombok.extern.slf4j.Slf4j;






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.repository.CourseRepository;

@Slf4j
@Service
@Transactional
public class CourseService {
	@Autowired
	private CourseRepository courseRepository;
	@Autowired
	private PageJdbcUtil pageJdbcUtil;
	public Page<Course>  getPage(Pageable pageable, String name,Long userId,String courseId) {
		Page<Course> page = null;
		if(!StringUtils.isEmpty(courseId)) {
			Course course = courseRepository.findOne(courseId);
			ArrayList<Course> courseList = new ArrayList<Course>();
			courseList.add(course);
			page = new PageImpl<Course>(courseList,pageable,1);
		}else{
			if(StringUtils.isEmpty(name)) {
				page = courseRepository.findAllByCreatedByAndDeleteFlagAndIsPublishAndIsArchiveOrderByCreatedDateDesc(pageable,userId,DataValidity.VALID.getState(),true,false);
			}else {
				page = courseRepository.findAllByCreatedByAndNameLikeIgnoreCaseAndDeleteFlagAndIsPublishAndIsArchiveOrderByCreatedDateDesc(pageable,userId,"%"+name+"%",DataValidity.VALID.getState(),true,false);
			}
		}
		return page;
	}
	
	public List<Course> findCourseByCreatedBy(Long createdBy){
		return courseRepository.findAllByCreatedByAndIsPublishAndDeleteFlagAndIsArchive(createdBy, true,DataValidity.VALID.getState(),false);
	}
	
	public List<Course> findCourseByCreatedByAndIsPublishAndIsArchive(Long createdBy,boolean isPublish,boolean isArchive){
		return courseRepository.findAllByCreatedByAndIsPublishAndDeleteFlagAndIsArchive(createdBy,isPublish,DataValidity.VALID.getState(),isArchive);
	}
	
	public Long countByTemplateId(Long templateId,Long createdBy) {
		return courseRepository.countByTemplateIdAndDeleteFlagAndCreatedBy(templateId, DataValidity.VALID.getState(),createdBy);
	}
	
	List<Course> findAllByCreatedByAndTemplateIdAndIsPublishAndIsArchive(Long createdBy,Long templateId,Boolean isPublish,Boolean isArchive){
		return courseRepository.findAllByCreatedByAndTemplateIdAndIsPublishAndDeleteFlagAndIsArchive(createdBy, templateId, isPublish, DataValidity.VALID.getState(), isArchive);
	}

	public List<Course> findByCreatedByAndDeleteFlag(Long userId){
		return  courseRepository.findByCreatedByAndDeleteFlag(userId,0);
	}
	public Course findById(String id){
		return courseRepository.findCourseById(id);
	}
	public List<Course> findMyCourse(Long createdBy){
		return courseRepository.findByIsPublishAndIsArchiveAndCreatedByAndDeleteFlagOrderByCreatedDateDesc(true,false,createdBy,DataValidity.VALID.getState());
	}
	RowMapper<String> name = new RowMapper<String>() {
		@Override
		public String mapRow(ResultSet resultSet, int i) throws SQLException {
			return resultSet.getString("image_name");
		}
	};
	public List<String> findCourseName(String imageName){
		String sql="SELECT DISTINCT cc.`name` image_name FROM c_course_chapter c LEFT JOIN c_course cc ON cc.id=c.course_id WHERE c.vm_name='"+imageName+"' and cc.delete_flag=0 AND c.delete_flag=0 UNION SELECT DISTINCT cc.`name` image_name FROM c_template_chapter c LEFT JOIN c_template cc ON cc.id=c.template_id WHERE c.vm_name='"+imageName+"' and cc.delete_flag=0 AND c.delete_flag=0 ";
		return pageJdbcUtil.getInfo(sql,name);
	}
}
